package cn.kailangtu.commontools;

import java.util.Calendar;

public class MyCalendar {
    private Calendar calendar;

    // 初始化当前时间
    public MyCalendar() {
        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
    }

    // 通过时间戳初始化
    public MyCalendar(long timestamp) {
        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
    }

    // 时间加减操作
    public void addYears(int years) {
        calendar.add(Calendar.YEAR, years);
    }

    public void addMonths(int months) {
        calendar.add(Calendar.MONTH, months);
    }

    public void addDays(int days) {
        calendar.add(Calendar.DAY_OF_MONTH, days);
    }

    public void addHours(int hours) {
        calendar.add(Calendar.HOUR_OF_DAY, hours);
    }

    public void addMinutes(int minutes) {
        calendar.add(Calendar.MINUTE, minutes);
    }

    public void addSeconds(int seconds) {
        calendar.add(Calendar.SECOND, seconds);
    }

    // 获取时间戳（毫秒）
    public long getTimestamp() {
        return calendar.getTimeInMillis();
    }

    // 获取时间字段（返回int类型）
    public int getYear() {
        return calendar.get(Calendar.YEAR);
    }

    public int getMonth() {
        return calendar.get(Calendar.MONTH) + 1; // 月份从0开始，+1修正
    }

    public int getDay() {
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public int getHour() {
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public int getMinute() {
        return calendar.get(Calendar.MINUTE);
    }

    public int getSecond() {
        return calendar.get(Calendar.SECOND);
    }

    // 重置为当前时间
    public void resetToCurrentTime() {
        calendar.setTimeInMillis(System.currentTimeMillis());
    }

    // Android环境建议删除main方法，这里仅保留作为示例
    public static void main(String[] args) {
        MyCalendar myCal = new MyCalendar();

        System.out.println("当前时间: " + myCal.getYear() + "-"
                + myCal.getMonth() + "-" + myCal.getDay());

        myCal.addDays(7);
        System.out.println("加7天后: " + myCal.getYear() + "-"
                + myCal.getMonth() + "-" + myCal.getDay());

        System.out.println("当前时间戳: " + myCal.getTimestamp());
    }
}